#include <ctype.h>
#include <iostream>
#include <cstdlib>
#include <unistd.h>

using namespace std;

int main(int argc, char **argv)
{
	int aflag = 0;
	int bflag = 0;
	char *cvalue = NULL;
	int c;

	opterr = 0;

	while ((c = getopt(argc, argv, "abc:")) != -1) {
		switch(c) {
			case 'a':
				aflag = 1;
				break;
			case 'b':
				bflag = 1;
				break;
			case 'c':
				cvalue = optarg;
				break;
			case '?':
				if (optopt == 'c') {
					cerr << "Option -" << optopt << " requires an argument." << endl;
				} else if (isprint(optopt)) {
					cerr << "Unknown option `-" << optopt << "`." << endl;
				} else {
					cerr << "Unknown option character `" << optopt << "`." << endl;
				}
				return 1;
			default:
				abort();
		}
	}
	
	cout << "aflag = " << aflag << ", bflag = " << bflag << ", cvalue = ";
	if (cvalue == NULL) 
		cout << "(null)";
	else
		cout << cvalue;
	cout << endl;
	
	for (int i = optind; i < argc; i++)
		cout << "Non-option argument " << argv[i] << endl;
	
	return 0;
}
